﻿using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Xml.Serialization;
namespace CoreModule
{
    /// <summary>
    /// 设备配置信息
    /// </summary>
    public class DeviceConfigurationInfo: INotifyPropertyChanged
    {
        public static readonly string  RecognitionDeviceConfigurationInfoFilePath= "RecognitionDeviceConfigurationInfo.xml";
        public static readonly string HisDeviceConfigurationInfoFilePath = "HisDeviceConfigurationInfo.xml";
        public event PropertyChangedEventHandler PropertyChanged;
        /// <summary>
        /// 
        /// </summary>
        public DeviceConfigurationInfo()
        {
            
        }
        
        public void Load(string filePath)
        {
            if (File.Exists(filePath))
            {
                try
                {
                    using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate))
                    {
                        var conf = (DeviceConfigurationInfo)new XmlSerializer(typeof(DeviceConfigurationInfo)).Deserialize(fileStream);
                        this.DisplayName = conf.DisplayName;
                        this.DeviceName = conf.DeviceName;
                        this.Description = conf.Description;
                        //this.HasConfiguration = conf.HasConfiguration;
                        //this.IsChecked = conf.IsChecked;
                    }
                }
                catch (Exception)
                {
                    File.Delete(filePath);
                }
            }
        }
        /// <summary>
        /// /
        /// </summary>
        public void Save(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            try
            {
                using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate))
                {
                    new XmlSerializer(typeof(DeviceConfigurationInfo)).Serialize(fileStream, this);
                }
            }
            catch (Exception)
            {
     
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo([AllowNull] DeviceConfigurationInfo other)
        {
            return this.DeviceName.CompareTo(other.DeviceName);
        }
         
        /// <summary>
        /// 显示名称
        /// </summary>
        public string DisplayName { get;  set; }
        /// <summary>
        /// 描述
        /// </summary>
        /// <param name="region"></param>
        public string Description { get;  set; }
        /// <summary>
        /// 设备名称（唯一）
        /// </summary>
        public string DeviceName { get;  set; }
         
        /// <summary>
        /// 是否启动
        /// </summary>
        public bool IsChecked
        {
            get;set;
        }
        /// <summary>
        /// 是否有配置
        /// </summary>
        public bool HasConfiguration { get; set; }
         
    }
}
